package com.obtk.byproperty.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.obtk.byproperty.dao.ActivityMapper;
import com.obtk.byproperty.pojo.Activity;
import com.obtk.byproperty.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public List<Activity> selectAll() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        String localAddr = request.getLocalAddr();
        int serverPort = request.getServerPort();
        List<Activity> activities = activityMapper.selectAll();
        for (Activity b : activities) {
            //获取ip地址和端口
            b.setActivityImage("http://" + localAddr + ":" + serverPort + "/activity/" + b.getActivityImage());
            System.out.println(b.getActivityImage());
        }
        return activities;
    }

    @Override
    public List<Activity> slectBookPage(String communityName) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        String localAddr = request.getLocalAddr();
        int serverPort = request.getServerPort();

        List<Activity> list = activityMapper.selectByName(communityName);
        for (Activity b : list) {
            //获取ip地址和端口
            b.setActivityImage("http://" + localAddr + ":" + serverPort + "/activity/" + b.getActivityImage());
            System.out.println(b.getActivityImage());
        }
        return list;
    }

    @Override
    public List<String> getActivityAllName() {
        return activityMapper.getActivityAllName();
    }

    @Override
    public List<Activity> search(String title,String communityName) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        String localAddr = request.getLocalAddr();
        int serverPort = request.getServerPort();
        List<Activity> activities = activityMapper.selectSearch(title,communityName);
        for (Activity b : activities) {
            //获取ip地址和端口
            b.setActivityImage("http://" + localAddr + ":" + serverPort + "/activity/" + b.getActivityImage());
            System.out.println(b.getActivityImage());
        }

        return activities;
    }

    @Override
    public List<Activity> selectActivity(String title,String communityName) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        String localAddr = request.getLocalAddr();
        int serverPort = request.getServerPort();

        List<Activity> activities = activityMapper.selectActivity(title, communityName);
        for (Activity b : activities) {
            //获取ip地址和端口
            b.setActivityImage("http://" + localAddr + ":" + serverPort + "/activity/" + b.getActivityImage());
            System.out.println(b.getActivityImage());
        }
        return activities;
    }

}
